package cn.sh.yhk;

import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * Redis 缓存工具类
 *
 * @Classname Yhk
 * @Description TODO
 * @Version 1.0.0
 * @Date 2024/10/16 13:46
 * @Created by y6522
 */
@Component
public class CacheUtils {

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    RedissonClient redissonClient;


    public void lock(String key) {
        redissonClient.getLock(key);
    }

    public void lock(String key, long time, TimeUnit unit) {
        redissonClient.getLock(key).lock(time, unit);
    }

    public boolean tryLock(String key, long waitTime, long leaseTime) throws InterruptedException {
        return redissonClient.getLock(key).tryLock(waitTime, leaseTime, java.util.concurrent.TimeUnit.SECONDS);
    }

    public void unlock(String key) {
        redissonClient.getLock(key).unlock();
    }

    public void expire(String key, long timeout) {
        redisTemplate.expire(key, timeout, java.util.concurrent.TimeUnit.SECONDS);
    }

    public void set(String key, Object value) {
        redisTemplate.opsForValue().set(key, value);
    }

    public void set(String key, Object value, long timeout) {
        redisTemplate.opsForValue().set(key, value, timeout, TimeUnit.SECONDS);
    }


    public void increment(String key) {
        redisTemplate.opsForValue().increment(key, 1);
    }

    public void decrement(String key) {
        redisTemplate.opsForValue().decrement(key, 1);
    }


    public Object get(String key) {
        return redisTemplate.opsForValue().get(key);
    }

    public boolean del(String key) {
        return redisTemplate.delete(key);
    }

    public void hset(String key, String field, Object value) {
        redisTemplate.opsForHash().put(key, field, value);
    }

    public Object hget(String key, String field) {
        return redisTemplate.opsForHash().get(key, field);
    }

    public void hdel(String key, String field) {
        redisTemplate.opsForHash().delete(key, field);
    }

    public void lpush(String key, Object value) {
        redisTemplate.opsForList().leftPush(key, value);
    }

    public Object lpop(String key) {
        return redisTemplate.opsForList().leftPop(key);
    }

    public Object lrange(String key, long start, long end) {
        return redisTemplate.opsForList().range(key, start, end);
    }

    public void sadd(String key, Object value) {
        redisTemplate.opsForSet().add(key, value);
    }

    public Object spop(String key) {
        return redisTemplate.opsForSet().pop(key);
    }

    public Object sismember(String key, Object value) {
        return redisTemplate.opsForSet().isMember(key, value);
    }

    public Object zadd(String key, Object value, double score) {
        return redisTemplate.opsForZSet().add(key, value, score);
    }

    public Object zrem(String key, Object value) {
        return redisTemplate.opsForZSet().remove(key, value);
    }

    public Object zrevrange(String key, long start, long end) {
        return redisTemplate.opsForZSet().reverseRange(key, start, end);
    }

    public Object zrevrangebyscore(String key, double min, double max) {
        return redisTemplate.opsForZSet().reverseRangeByScore(key, min, max);
    }

    public Object zcard(String key) {
        return redisTemplate.opsForZSet().size(key);
    }

    public Object zscore(String key, Object value) {
        return redisTemplate.opsForZSet().score(key, value);
    }

    public Object zremrangebyrank(String key, long start, long end) {
        return redisTemplate.opsForZSet().removeRange(key, start, end);
    }

    public Object zremrangebyscore(String key, double min, double max) {
        return redisTemplate.opsForZSet().removeRangeByScore(key, min, max);
    }

    public Object zcount(String key, double min, double max) {
        return redisTemplate.opsForZSet().count(key, min, max);
    }

    public Object zincrby(String key, double score, Object value) {
        return redisTemplate.opsForZSet().incrementScore(key, value, score);
    }

}
